{include file="public_iframe:head"}
<div class="wrapper wrapper-content animated fadeInRight">
	<div class="ibox float-e-margins">
		<div class="ibox-title">
			<h5>权限资源管理 - 编辑权限</h5>
			<div class="ibox-tools" style="margin-top: -9px;">
				<button onclick="history.go(-1);" class="btn btn-primary"><i class="fa fa-arrow-left"></i> 返回</button>
			</div>
		</div>
		<div class="ibox-content">
			<form action="" method="post" enctype="multipart/form-data" id="adminHandle" class="form-horizontal">
				{if condition="isset($data.id)"}
					<input type="hidden" name="id" value="{$data.id}">
				{/if}
				<div class="form-group">
					<label class="col-sm-2 control-label">权限资源名称</label>

					<div class="col-sm-6">
						<input type="text" class="form-control"  {if condition="isset($data.name)"} value="{$data.name}" {/if}name="name" id="name" class="input-txt" placeholder="权限资源名称...">
					</div>
				</div>
				<div class="hr-line-dashed"></div>
				<div class="form-group">
					<label class="col-sm-2 control-label">所属分组</label>

					<div class="col-sm-6">
						<select class="form-control m-b"  id="group" name="group">
							{foreach name="group" item="vo" key="key"}
								<option value="{$key}" {if condition="isset($data['group']) && $data['group'] eq $key"}selected{/if}>{$vo}</option>
							{/foreach}
						</select>
					</div>
				</div>
				<div class="hr-line-dashed"></div>
				<div class="form-group">
					<label class="col-sm-2 control-label">添加权限码</label>

					<div class="col-sm-2">
						<select class="form-control m-b" id="controller" onchange="get_act_list(this)">
							<option value="">选择控制器</option>
							{foreach name="planList" item="vo"}
								<option value="{$vo}">{$vo}</option>
							{/foreach}
						</select>
					</div>
					<div class="col-sm-2">
						<select class="form-control m-b"  id="act_list">
							<option value="">选择方法</option>
							{foreach name="planList" item="vo"}
								<option value="{$vo}">{$vo}</option>
							{/foreach}
						</select>
					</div>
					<div class="col-sm-1">
						<a class="ncap-btn" onclick="add_right()">添加</a>
					</div>

				</div>
				<div class="hr-line-dashed"></div>
				<div class="form-group">
					<label class="col-sm-2 control-label">权限码</label>
					<div class="col-sm-6">
						<table data-toggle="table" class="table table-bordered table-hover">
							<thead><tr><th>权限码</th><th>操作</th></tr></thead>
							<tbody id="rightList">
							{if condition="isset($data.right)"}
								{foreach name="$data['right']" item="vo"}
									<tr><td><input name="right[]" type="text" value="{$vo}" class="form-control" style="width:300px;"></td>
										<td style="text-align: center;"><a class="ncap-btn" href="javascript:;" onclick="$(this).parent().parent().remove();">删除</a></td></tr>
								{/foreach}
							{/if}
							</tbody>
						</table>
					</div>
				</div>
				<div class="hr-line-dashed"></div>
				<div class="form-group">
					<div class="col-sm-4 col-sm-offset-2">
						<a href="JavaScript:void(0);" onclick="adsubmit();" class="btn btn-primary" id="submitBtn">确认提交</a>
					</div>
				</div>
			</form>
		</div>
	</div>
</div>
<script src="__STATIC__/admin/js/jquery.min.js?v=2.1.4"></script>
<script type="text/javascript">
	function add_right(){
		var a = [];
		$('#rightList .form-control').each(function(i,o){
			if($(o).val() != ''){
				a.push($(o).val());
			}
		})
		var ncode = $('#controller').val();
		if(ncode !== ''){
			var temp = ncode+'@'+ $('#act_list').val();
			if($.inArray(temp,a) != -1){
				parent.layer.msg('此权限码已经添加！', {icon: 2,time: 1000,shade: false});
				return false;
			}
		}
		var strtr = '<tr>';
		if(ncode!= ''){
			strtr += '<td><input type="text" name="right[]" value="'+ncode+'@'+ $('#act_list').val()+'" class="form-control" style="width:300px;"></td>';
		}else{
			strtr += '<td><input type="text" name="right[]" value="" class="form-control" style="width:300px;"></td>';
		}
		strtr += '<td style="text-align: center;"><a href="javascript:;" class="ncap-btn" onclick="$(this).parent().parent().remove();">删除</a></td>';
		$('#rightList').append(strtr);
	}
	function get_act_list(obj){
		$.ajax({
			url: "{:url('admin/ajax_get_action')}",
			type:'get',
			data: {'controller':$(obj).val()},
			dataType:'html',
			success:function(res){
				$('#act_list').empty().append(res);
			}
		});
	}
	function adsubmit(){
		if($('input[name=name]').val() == ''){
			parent.layer.msg('权限名称不能为空！', {icon: 2,time: 1000,shade: false,});
			return false;
		}

		if($('input[name="right\[\]"]').length == 0){
			parent.layer.msg('权限码不能为空！', {icon: 2,time: 1000,  shade: false});
			return false;
		}

		$('#adminHandle').submit();
	}
</script>
</body>
</html>